﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GzipManagerPro.IO
{
    [Serializable]
    public partial class Folder: ArchiveItem
    {
        
        private List<File> _Files;
        private List<Folder> _Folders;

        public List<File> Files
        {
            get
            {
                if (_Files == null)
                    _Files = new List<File>();

                return _Files;
            }
            set
            {
                _Files = value;
            }
        }

        public List<Folder> Folders
        {
            get
            {
                if (_Folders == null)
                    _Folders = new List<Folder>();

                return _Folders;
            }
            set
            {
                _Folders = value;
            }
        }

        public static Folder FromPath(string path)
        {
            return FromPath(path, true);
        }

        public static Folder FromPath(string path, bool includeSubFolders)
        {
            if (!System.IO.Directory.Exists(path))
                throw new System.IO.DirectoryNotFoundException();

            Folder result = new Folder()
            {
                Name = System.IO.Path.GetFileName(path)
            };


            // Adding files
            string[] files = System.IO.Directory.GetFiles(path);
            foreach (string file in files)
                result.Files.Add(File.FromPath(file));

            // Adding sub folders
            if (includeSubFolders)
            {
                string[] directories = System.IO.Directory.GetDirectories(path);
                foreach (string directory in directories)
                    result.Folders.Add(Folder.FromPath(directory, includeSubFolders));
            }
            return result;
        }

        public int GetFilesCount()
        {
            int result = this.Files.Count;
            this.Folders.ForEach(f => result += f.GetFilesCount());
            return result;
        }
    }
}
